package com.hgyc.mom.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgyc.mom.base.entity.MaterialType;
import com.hgyc.mom.base.service.MaterialTypeService;
import com.hgyc.mom.base.vo.MaterialTypeParam;
import com.hgyc.mom.base.mapper.MaterialTypeMapper;
import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 物料类型信息 服务实现类
 * </p>
 *
 * @author fwj
 * @since 2025-03-24
 */
@Service
public class MaterialTypeServiceImpl extends ServiceImpl<MaterialTypeMapper, MaterialType> implements MaterialTypeService {

    @Override
    public List<MaterialType> treeList(MaterialTypeParam materialTypeParam) {
        MaterialType materialType = new MaterialType();
        BeanUtils.copyProperties(materialTypeParam, materialType);
        QueryWrapper<MaterialType> wrapper = QueryWrapperUtil.build(materialType);
        List<MaterialType> list = list(wrapper);
        return this.convertToTree(list);
    }

    private List<MaterialType> convertToTree(List<MaterialType> materialTypeList) {
        Map<Long, List<MaterialType>> parentChildrenMap = new HashMap<>();
        for (MaterialType materialType : materialTypeList) {
            Long parentId = materialType.getParentId();

            // 将当前菜单项添加到父菜单项的子菜单项列表中
            List<MaterialType> children = parentChildrenMap.getOrDefault(parentId, new ArrayList<>());
            children.add(materialType);
            parentChildrenMap.put(parentId, children);

        }
        // 将根节点的子菜单项列表转换为树形结构
        List<MaterialType> rootChildren = parentChildrenMap.getOrDefault(0L, new ArrayList<>());
        for (MaterialType type : rootChildren) {
            convertToTree(type, parentChildrenMap);
        }
        return rootChildren;
    }

    private void convertToTree(MaterialType parent, Map<Long, List<MaterialType>> parentChildrenMap) {
        Long parentId = parent.getId();
        List<MaterialType> children = parentChildrenMap.getOrDefault(parentId, new ArrayList<>());
        parent.setChildren(children);
        for (MaterialType child : children) {
            convertToTree(child, parentChildrenMap);
        }
    }

    @Override
    public void addNode(MaterialType materialType) {
        MaterialType parent = null;
        if (materialType.getParentId() == null) {
            materialType.setParentId(0L);
        } else {
            parent = getById(materialType.getParentId());
        }

        //设置层级路径
        if (parent == null) {
            materialType.setPath(materialType.getTypeCode());
        } else {
            materialType.setPath(parent.getPath() + "/" + materialType.getTypeCode());
        }
        save(materialType);
    }

    @Override
    public MaterialType getByName(String type) {
        LambdaQueryWrapper<MaterialType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MaterialType::getTypeName, type);
        return getOne(wrapper, false);
    }
}
